//js获取项目根路径，如： http://localhost:8083/uimcardprj
function getRootPath(){
    //获取当前网址，如： http://localhost:8083/uimcardprj/share/meun.jsp
    var curWwwPath=window.document.location.href;
    //获取主机地址之后的目录，如： uimcardprj/share/meun.jsp
    var pathName=window.document.location.pathname;
    var pos=curWwwPath.indexOf(pathName);
    //获取主机地址，如： http://localhost:8083
    var localhostPaht=curWwwPath.substring(0,pos);
    //获取带"/"的项目名，如：/uimcardprj
    var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);
    return(localhostPaht + projectName);
}
var contextPath = getRootPath();    //应用跟路径
var testPaperVm = new Vue({
    el: "#testPaperDiv",
    data:{
        paperTitle: "",		//题目
        questionList:[]     //问题列表
    },
    methods:{
        saveTestPaper: function(){
            if(!this.paperTitle){
                $("body").MessageBox({
                    type: 'error',
                    message:'标题不能为空',
                    timeout:3000,
                    callbak:null
                });
                return;
            }

            const url = getRootPath() + '/testPaperManager/addOneTestPaper' ;	//默认选择50提      contextPath
            const data = {}
            data.questionList = this.questionList;
            data.title = this.paperTitle
            console.log(data);
            axios.post(url, data).then(
                response => {
                    console.log(response.data); //保存结果
                    //保存成功，跳转到列表页面
                    window.location.href = contextPath + "/testPaperManager/testPaperList";
                }).catch(response => {
                    alert('请求失败');
                });

            return false;
        },
        randomSelect: function(){
            const url = getRootPath() + '/questionManager/randomSelectQuestions/' + 20;	//暂定抽取20道题目      contextPath
            axios.get(url).then(
                response => {
                const dataLsit = response.data.questionList;
                let tempList = [];   //清空数组
                dataLsit && dataLsit.forEach((item, index) => {
                    tempList[index] = {
                        id: item.id,
                        order: index + 1,
                        content: item.content,
                        question:{ id: item.id }
                    };
                });
                // console.log(tempList);
                this.questionList = tempList;   //不能直接在forEach中为this.questionList设置元素，不会刷新界面。
            }).catch(
                    response => {
                    alert('请求失败');
            });
        },
        manualSelect: function(){
            alert("手动选题" + this.paperTitle);
        }
    }
});

/* 保存方法 */
function validateForm(){
    var title = $("#title").val();
    if(title == null || title==''){
        $("body").MessageBox({
            type: 'error',
            message:'标题不能为空',
            timeout:3000,
            callbak:null
        });
        return false;
    }
    return true;
}